<template>
  <el-card class="box-card">
    <template #header>
      <div class="card-header">
        <span>商品服务</span>
      </div>
    </template>
    <el-row :gutter="20" justify="space-between">
      <el-col :span="20">
        <el-select
          style="margin-right: 7px"
          v-model="pageDate.bigTypeId"
          placeholder="商品类型"
          clearable
          @change="bigChange"
        >
          <el-option
            v-for="item in bigTypeList"
            :key="item.val"
            :label="item.name"
            :value="item.val"
          />
        </el-select>

        <el-select
          v-model="pageDate.status"
          placeholder="商品状态"
          clearable
          @change="selectChange"
        >
          <el-option
            v-for="item in statusList"
            :key="item.val"
            :label="item.name"
            :value="item.val"
          />
        </el-select>
      </el-col>
      <el-col :span="2">
        <el-button type="primary" class="height40" @click="createService"
          >新建服务</el-button
        >
      </el-col>
    </el-row>
    <el-table
      :data="tableData"
      style="width: 100%"
      class="margintop"
      stripe
      v-loading="tableLoading"
      :header-cell-style="{ 'border-bottom': 'none' }"
      :header-row-style="{ height: '60px' }"
      :cell-style="{ height: '60px', 'border-bottom': 'none' }"
    >
      <el-table-column type="index" label="排序" width="150" align="center" />
      <el-table-column prop="name" label="商品名称" />
      <el-table-column prop="bigTypeName" label="商品类型" align="center" />
      <el-table-column prop="basePrice" label="销售价" align="center" />
      <el-table-column prop="realPrice" label="折扣价" align="center" />
      <el-table-column prop="sellNum" label="销售量" align="center" />
      <el-table-column prop="status" label="商品状态" align="center">
        <template #default="scope">
          <span>{{ scope.row.status === 0 ? '下架' : '上架' }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="type" label="操作" align="center">
        <template #default="scope">
          <el-tooltip
            class="box-item"
            effect="dark"
            content="编辑"
            placement="top"
          >
            <el-icon
              size="20"
              style="cursor: pointer"
              @click="editChange(scope.row)"
            >
              <EditPen />
            </el-icon>
          </el-tooltip>

          <el-tooltip
            class="box-item"
            effect="dark"
            :content="scope.row.status === 0 ? '上架' : '下架'"
            placement="top"
          >
            <el-icon
              size="20"
              style="margin: 0 10px"
              @click="downChange(scope.row)"
            >
              <Top class="curr" v-if="scope.row.status == 0" />
              <Bottom class="curr" v-else />
            </el-icon>
          </el-tooltip>

          <el-tooltip
            class="box-item"
            effect="dark"
            content="删除"
            placement="top"
          >
            <el-icon size="20" style="cursor: pointer; margin-left: 10px">
              <Delete @click="deleteChange(scope.row)" />
            </el-icon>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      v-model:page="pageDate.page"
      v-model:limit="pageDate.pageSize"
      :total="total"
      :background="false"
      @pagination="paginationChange"
    ></pagination>
  </el-card>
</template>
<script setup>
import { ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage, ElMessageBox } from 'element-plus';
import Pagination from '@/components/Pagination/index.vue';
import {
  servicesList,
  servicesListFilter,
  servicesDel,
  servicesDown,
} from '@/api/service/service.js';
const tableLoading = ref(false);
const router = useRouter();
const tableData = ref([]); // 保存商品列表
const pageDate = ref({
  page: 1,
  pageSize: 10,
  status: '',
  bigTypeId: '',
});
const total = ref(0);
const bigTypeList = ref([]);
const statusList = ref([]);

onMounted(() => {
  getGoodsService();
  servicesListChange();
});

function bigChange() {
  servicesListChange();
}

function selectChange() {
  servicesListChange();
}
const paginationChange = (val) => {
  pageDate.pageSize = val.limit;
  servicesListChange();
};

function downChange(val) {
  let goods = val.status === 1 ? 0 : 1;
  const date = {
    id: val.id,
    status: goods,
  };
  servicesDown(date).then((res) => {
    ElMessage.success(res.msg);
    servicesListChange();
  });
}

function getGoodsService() {
  servicesListFilter().then((res) => {
    bigTypeList.value = res.data[0].data;
    statusList.value = res.data[1].data;
  });
}

function servicesListChange() {
  tableLoading.value = true;
  servicesList(pageDate.value)
    .then((res) => {
      if (res.code === 20000) {
        tableData.value = res.data.data;
        total.value = res.data.total;
        tableLoading.value = false;
      } else {
        tableLoading.value = false;
      }
    })
    .catch(() => {
      tableLoading.value = false;
    });
}

function deleteChange(val) {
  ElMessageBox.confirm('是否删除当前列表数据信息', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: '提示',
  })
    .then(() => {
      servicesDel({
        id: val.id,
      }).then(() => {
        ElMessage({
          type: 'success',
          message: '删除成功',
        });
        servicesListChange();
      });
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '取消删除',
      });
    });
}

function createService() {
  router.push('/operation/create');
}

function editChange(val) {
  router.push({
    path: '/operation/goodServce',
    query: {
      id: val.id,
    },
  });
}
</script>
<style lang="scss" scoped>
::v-deep(.el-form--inline .el-form-item) {
  margin-right: 7px;
}

::v-deep(.el-form-item--default) {
  margin-bottom: 4px;
}

.box-card {
  margin: 20px;

  .card-header {
    font-size: 20px;
    font-weight: 600;
    color: #1d2129;
    padding: 10px;

    .headerSize {
      color: #4e5969;
      font-size: 14px;
      margin-left: 12px;
      font-weight: 400;
      height: 20px;
      line-height: 21px;
    }
  }
}
</style>
